package lanqiao;
/**
 * @Project : ProgrammingExercises
 * @Package : LanQiao.PrepareForCompetition
 * @File : Q0180.java
 * @Author : WangRuoyu
 * @Date : 2023/3/18 8:50
 */

import java.io.*;

public class Q0180 {
    static int m;
    static int n;
    static int l;
    static long[][][] b;
    static long[][][] tmp;

    static int[][] op;

    static boolean check(int x) {
        long[][][] b2 = new long[m + 2][n + 2][l + 2];

        for (int i = 0; i <= m + 1; i++) {
            for (int j = 0; j <= n + 1; j++) {
                for (int k = 0; k <= l + 1; k++) {
                    b2[i][j][k] = b[i][j][k];
                }
            }
        }

        for (int i = 0; i < x; i++) {
            insert(b2, op[i][0], op[i][2], op[i][4], op[i][1], op[i][3], op[i][5], -op[i][6]);
        }

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                for (int k = 1; k <= l; k++) {
                    tmp[i][j][k] = b2[i][j][k] + tmp[i - 1][j][k] + tmp[i][j - 1][k] + tmp[i][j][k - 1] - tmp[i - 1][j - 1][k] - tmp[i][j - 1][k - 1] - tmp[i - 1][j][k - 1] + tmp[i - 1][j - 1][k - 1];
                    if (tmp[i][j][k] < 0) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    static void insert(long[][][] ary, int x1, int y1, int z1, int x2, int y2, int z2, int c) {
        ary[x1][y1][z1] += c;
        ary[x2 + 1][y1][z1] -= c;
        ary[x1][y2 + 1][z1] -= c;
        ary[x1][y1][z2 + 1] -= c;
        ary[x2 + 1][y1][z2 + 1] += c;
        ary[x1][y2 + 1][z2 + 1] += c;
        ary[x2 + 1][y2 + 1][z1] += c;
        ary[x2 + 1][y2 + 1][z2 + 1] -= c;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] words = br.readLine().split(" ");

        m = Integer.parseInt(words[0]);
        n = Integer.parseInt(words[1]);
        l = Integer.parseInt(words[2]);
        int p = Integer.parseInt(words[3]);

        b = new long[m + 2][n + 2][l + 2];
        tmp = new long[m + 1][n + 1][l + 1];

        int index = 0;
        words = br.readLine().split(" ");
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                for (int k = 1; k <= l; k++) {
                    insert(b, i, j, k, i, j, k, Integer.parseInt(words[index++]));
                }
            }
        }

        op = new int[p][7];
        for (int i = 0; i < p; i++) {
            words = br.readLine().split(" ");
            for (int j = 0; j < 7; j++) {
                op[i][j] = Integer.parseInt(words[j]);
            }
        }

        int ll = 0;
        int rr = p - 1;
        while (ll < rr) {
            int mid = (ll + rr) >> 1;
            if (check(mid)) {
                rr = mid;
            } else {
                ll = mid + 1;
            }
        }

        System.out.println(ll);
    }
}


